package com.xmzs.web.controller;


import com.thoughtworks.xstream.core.SecurityUtils;
import com.xmzs.common.chat.domain.request.SaveMsgRequest;
import com.xmzs.common.chat.entity.chat.BaseChatCompletion;
import com.xmzs.common.chat.domain.request.ChatRequest;
import com.xmzs.common.chat.utils.TikTokensUtil;
import com.xmzs.common.core.domain.R;
import com.xmzs.common.core.domain.model.LoginUser;
import com.xmzs.common.core.exception.base.BaseException;
import com.xmzs.common.mybatis.core.page.PageQuery;
import com.xmzs.common.mybatis.core.page.TableDataInfo;
import com.xmzs.common.satoken.utils.LoginHelper;
import com.xmzs.system.domain.bo.ChatMessageBo;
import com.xmzs.system.domain.bo.SysNoticeBo;
import com.xmzs.system.domain.vo.ChatMessageVo;
import com.xmzs.system.domain.vo.SysNoticeVo;
import com.xmzs.system.service.IChatMessageService;


import com.xmzs.web.service.ChatService;
import com.xmzs.web.service.SseService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;

import java.math.BigDecimal;
import java.util.List;

/**
 * 描述：
 *
 * @author https:www.unfbx.com
 * @date 2023-03-01
 */
@Controller
@Slf4j
@RequiredArgsConstructor
public class ChatController {

    private final SseService sseService;

    private final ChatService chatService;

    private final  IChatMessageService chatMessageService;


    /**
     * 聊天接口
     */
    @PostMapping("/chat")
    @ResponseBody
    public ResponseBodyEmitter sseChat(@RequestBody @Valid ChatRequest chatRequest, HttpServletResponse response) {
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        return sseService.sseChat(chatRequest);
    }

    /**
     * 聊天记录
     */
    @PostMapping("/chatList")
    @ResponseBody
    public R<TableDataInfo<ChatMessageVo>> list(@RequestBody @Valid ChatMessageBo chatRequest,@RequestBody PageQuery pageQuery) {
        // 默认查询当前登录用户消息记录
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new BaseException("用户未登录！");
        }
        chatRequest.setUserId(loginUser.getUserId());
        TableDataInfo<ChatMessageVo> chatMessageVoTableDataInfo = chatMessageService.queryPageList(chatRequest, pageQuery);
        return R.ok(chatMessageVoTableDataInfo);
    }

    @CrossOrigin
    @PostMapping("/saveMsg")
    @ResponseBody
    public void saveMsg(@RequestBody @Valid SaveMsgRequest saveMsgRequest) {
        // 计算tokens数量
        int tokens = TikTokensUtil.tokens(saveMsgRequest.getModel(), saveMsgRequest.getMsg());
        // 根据消耗的tokens扣除余额
        chatService.deductToken(saveMsgRequest.getModel(),tokens,BaseChatCompletion.ChatType.CHAT_OUT.name(),saveMsgRequest.getMsg(), BigDecimal.ZERO);
    }

}
